//用户相关的仓库
import { defineStore } from "pinia";
import {reqUserLogin  } from "@/api/user";
import { GET_TOKEN, SET_TOKEN, REMOVE_TOKEN } from "@/utils/user";
const useUserStore = defineStore('User', {
    state: () => {
        return {
            
            userToken:GET_TOKEN() as string,
            currentUser: JSON.parse(localStorage.getItem("currentUser") || '{}'),

            
        }
    },
    actions: {
        loadCurrentUser(user:any) {
            this.currentUser = user;
            console.log(this.currentUser);
            
            localStorage.setItem("currentUser", JSON.stringify(user));
          },
          loadUserToken(userToekn:any) {
            this.userToken = userToekn;
            localStorage.setItem("userToken", JSON.stringify(userToekn));
          },
        async userLogin(loginParam:any) {
            let result: any = await reqUserLogin(loginParam)
            if (result.code == 200) {

                this.loadCurrentUser(result.data)
                localStorage.setItem("userToken", result.data.accessToken);

                this.userToken = result.data.accessToken
                return this.userToken
            } else {
                return Promise.reject(new Error(result.message))
            }
        },

    },
    getters: {

    }
})

export default useUserStore;